package com.ostrich.cloud.ostrichGoodsService.app.ApiImp;

import cn.hutool.core.bean.BeanUtil;
import com.ostrich.cloud.common.result.PageResult;
import com.ostrich.cloud.common.result.Result;
import com.ostrich.cloud.ostrichGoodsSercice.model.Dto.GoodsDto;
import com.ostrich.cloud.ostrichGoodsSercice.model.param.CreateGoodsParam;
import com.ostrich.cloud.ostrichGoodsSercice.model.param.GetGoodsParam;
import com.ostrich.cloud.ostrichGoodsSercice.model.param.PutGoodsParam;
import com.ostrich.cloud.ostrichGoodsService.app.component.Utlis;
import com.ostrich.cloud.ostrichGoodsService.client.GoodsClient;
import com.ostrich.cloud.ostrichGoodsService.service.GoodsService;
import com.ostrich.cloud.ostrichGoodsService.service.param.AddGoodsParam;
import com.ostrich.cloud.ostrichGoodsService.service.param.SelectGoodsParam;
import com.ostrich.cloud.ostrichGoodsService.service.param.UpdateGoodsParam;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class GoodsApiImp implements GoodsClient {

    @Resource
    private GoodsService goodsService;


    @Override
    public GoodsDto getGoodsDto(Integer goodsId) {
        GoodsDto goodsPrice = goodsService.getGoodsDto(goodsId);

        return goodsPrice;
    }

    @Override
    public boolean createGoods(CreateGoodsParam param) {
        Integer businessmanId = Utlis.getParentId();
        AddGoodsParam addGoodsParam = BeanUtil.copyProperties(param, AddGoodsParam.class);
        if(addGoodsParam.getBusinessmanId()==null){
            addGoodsParam.setBusinessmanId(businessmanId);
        }
        Boolean bool=goodsService.addGoods(addGoodsParam);
        return bool;
    }

    @Override
    public PageResult<GoodsDto> getAllGoods(Integer current, Integer size) {
        PageResult<GoodsDto> goodsDtoPageResult = goodsService.selectAllGoodsDto(current, size);
        return goodsDtoPageResult;
    }

    @Override
    public PageResult<GoodsDto> getGoodsByCriteria(GetGoodsParam param) {
        SelectGoodsParam selectGoodsParam = BeanUtil.copyProperties(param, SelectGoodsParam.class);
        if(selectGoodsParam.getBusinessmanId()==null){
            selectGoodsParam.setBusinessmanId(Utlis.getParentId());
        }
        PageResult<GoodsDto> goodsDtoPageResult = goodsService.selectGoodsDto(selectGoodsParam);

        return goodsDtoPageResult;
    }



    @Override
    public boolean putGoods(PutGoodsParam param) {

        UpdateGoodsParam updateGoodsParam = BeanUtil.copyProperties(param, UpdateGoodsParam.class);

        boolean bool= goodsService.updateGoods(updateGoodsParam);

        return bool;
    }

    @Override
    public boolean delGoods(Integer goodsId) {

        boolean bool=goodsService.delGoods(goodsId);

        return bool;

    }

    @Override
    public Result HotGoods() {
        List<GoodsDto> hotGoods = goodsService.getHotGoods();
        return Result.ok(hotGoods);
    }
}
